<script setup lang="ts" name="DeptEdit">

import { addDeptApi, getDeptApi, listDeptApi, updateDeptApi } from "@/api/system/dept";

const { proxy } = getCurrentInstance();

const { sys_normal_disable } = useDict("sys_normal_disable")

const emit = defineEmits(['closeDialog']);

const props = defineProps({
  id: {
    type: Number,
    default: null,
  },
  parentId: {
    type: Number,
    default: null
  },
  title: {
    type: String,
    default: false,
  }
})

// 部门管理表单
const deptFormRef = ref(null);

// 部门列表
const deptList = ref<DeptRecord[]>([])

// 弹窗开关
const open = ref(false);

// 表单数据
const form = ref<DeptRecord>({});

// 表单校验
const rules = ref({
  parentId: [
    { required: true, message: "上级部门不能为空", trigger: "blur" },
  ],
  deptName: [
    { required: true, message: "部门名称不能为空", trigger: "blur" },
  ],
  orderNum: [
    { required: true, message: "显示排序不能为空", trigger: "blur" },
  ],
  phone: [
    {pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: "请输入正确的手机号码", trigger: "blur"}
  ],
  email: [
    {type: "email", message: "请输入正确的邮箱地址", trigger: ["blur", "change"]},
  ],
  status: [
    { required: true, message: "部门状态不能为空", trigger: "blur" },
  ],
});

// 加载
const loading = ref(false);

/** 获取部门管理详情 */
function getData() {
  reset();
  if (props.id || props.id === 0) {
    getDeptApi(props.id).then(response => {
      form.value = response.data;
      open.value = true;
    }).catch(() => {
      closeDialog(false);
    })
  } else {
    form.value.parentId = props.parentId;
    open.value = true;
  }
}

// 获取部门列表
function getDeptList() {
  listDeptApi().then(response => {
    deptList.value = handleTree(response.data, "deptId");
  });
}

/** 表单重置 */
function reset() {
  form.value = {
    deptId: null,
    parentId: null,
    deptName: null,
    orderNum: 0,
    leader: null,
    phone: null,
    email: null,
    status: SYS_NORMAL_DISABLE_ENUM.normal,
  };
}

/** 关闭弹窗 */
function closeDialog(refresh: boolean) {
  open.value = false;
  emit("closeDialog", refresh);
}

/** 提交按钮 */
function submitForm() {
  // @ts-ignore
  deptFormRef.value.validate((valid: boolean) => {
    if (valid) {
      loading.value = true;
      if (form.value.deptId) {
        updateDeptApi(form.value).then(() => {
          // @ts-ignore
          proxy.$modal.msgSuccess("修改成功");
          loading.value = false;
          closeDialog(true);
        }).catch(() => {
          loading.value = false;
        });
      } else {
        addDeptApi(form.value).then(() => {
          // @ts-ignore
          proxy.$modal.msgSuccess("新增成功");
          loading.value = false;
          closeDialog(true);
        }).catch(() => {
          loading.value = false
        });
      }
    }
  })
}

watch(open, (val) => {
  if (!val) {
    closeDialog(false);
  }
})

getData();
getDeptList();
</script>

<template>
  <!-- 部门管理编辑对话框 -->
  <el-dialog :title="title" v-model="open" width="700px" append-to-body :close-on-click-modal="false">
    <el-form ref="deptFormRef" :model="form" :rules="rules" :inline="true" label-width="80px">
      <el-form-item label="上级部门" prop="parentId">
        <el-tree-select
            v-model="form.parentId"
            :data="deptList"
            :props="{ value: 'deptId', label: 'deptName', children: 'children' }"
            value-key="deptId"
            placeholder="选择上级部门"
            check-strictly
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model="form.deptName" placeholder="请输入部门名称" />
      </el-form-item>
      <el-form-item label="显示排序" prop="orderNum">
        <el-input-number v-model="form.orderNum" :min="0" label="请输入显示排序" />
      </el-form-item>
      <el-form-item label="负责人" prop="leader">
        <el-input v-model="form.leader" placeholder="请输入负责人" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="form.phone" placeholder="请输入联系电话" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入邮箱" />
      </el-form-item>
      <el-form-item label="部门状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio
              :value="item.value"
              v-for="(item, index) in sys_normal_disable"
              :key="index"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" v-debounce="submitForm" :loading="loading">确 定</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>

</style>
  